﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto.WHOMDto;
using TR.Domain.Entity.WHOB;
using TR.Domain.Repository.WHOM_SelectRepository;

namespace TR.EFCore.Repository.WHOM_SelectRepository
{
    public class WHOM_SelectRepository : IWHOB_SelectRepository
    {
        private readonly MyContext context;
        private int fyzts;

        public WHOM_SelectRepository(MyContext context)
        {
            this.context = context;
        }

        public async Task<List<WHOB_Select>> WHCZ(int dqy, int mys, string? qsj, string? hsj, WHOM_SelectDto ws)
        {
            //声明linq语法
            var query = context.WHOM_Select.AsQueryable();

            query = query.Where(p => p.is_del == 0);

            if (!string.IsNullOrEmpty(qsj) && !string.IsNullOrEmpty(hsj))
            {
                query = query.Where(p => string.Compare(p.buy_date, qsj) >= 0
                               && string.Compare(p.buy_date, hsj) <= 0);
            }

            //根据条件查询 进货单号、单据状态、供应商
            if (ws != null)
            {
                //单据编号
                if (!string.IsNullOrEmpty(ws.buy_code))
                {
                    query = query.Where(p => EF.Functions.Like(p.buy_code, "%" + ws.buy_code + "%"));
                }
                if (ws.sure_state != '\0')
                {
                    query = query.Where(p => p.sure_state == ws.sure_state);
                }
                if (!string.IsNullOrEmpty(ws.supply_name))
                {
                    query = query.Where(p => p.supply_name == ws.supply_name);
                }
            }
            var result = await query.OrderByDescending(p => p.create_time).Skip(dqy * (mys - 1)).Take(dqy).ToListAsync();//异步使用ToListAsync

            fyzts = await query.CountAsync(); // 获取符合查询条件的记录数

            return result;
        }

        //查询总数
        public async Task<int> WHShu()
        {
            return fyzts;
        }

        //逻辑删除 多条
        public async Task<bool> WHOBDeleteAsync(List<int> isdel)
        {
            try
            {
                var query = context;

                var wsDelete = query.wh_Buy_Order_H.Where(p => isdel.Contains(p.Id)).ToList();

                foreach (var id in wsDelete)
                {
                    id.is_del = 1;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        
        //批量修改状态
        public async Task<bool> StaticUpdateAsync(List<int> Bian, char zt)
        {
            try
            {
                var query = context;

                var staticUpdate = query.wh_Buy_Order_H.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in staticUpdate)
                {
                    id.sure_state = zt;
                    query.Entry(id).State= EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch 
            {
                Console.WriteLine("不对不对");
                return false;
            }
        }

        //批量审核
        public async Task<bool> SHUpdateAsync(List<int> Bian, char states, string sureman, string suredate, string remark)
        {
            try
            {
                var query = context;

                var ShenHeUpdate = query.wh_Buy_Order_H.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in ShenHeUpdate)
                {
                    id.sure_state = states;
                    id.sure_man = sureman;
                    id.sure_date = suredate;
                    id.remark = remark;
                    query.Entry(id).State = EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }

        //批量撤销
        public async Task<bool> CXUpdateAsync(List<int> Bian)
        {
            try
            {
                var query = context;

                var CheXiaoUpdate = query.wh_Buy_Order_H.Where(p => Bian.Contains(p.Id)).ToList();

                foreach (var id in CheXiaoUpdate)
                {
                    id.sure_state = '2';
                    id.sure_man = "";
                    id.sure_date = "";
                    id.remark = "";
                    query.Entry(id).State= EntityState.Modified;
                }
                query.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误");
                return false;
            }
        }
        //单号逻辑删除
        public async Task<bool> DHDelete(int Bian)
        {
            try
            {
                var query = context;

                var dhDelete = query.wh_Buy_Order_H.Where(p => p.Id == Bian);

                foreach (var id in dhDelete)
                {
                    id.is_del = 1;
                    query.Entry(id).State = EntityState.Modified;
                }
                await query.SaveChangesAsync();

                return true;
            }
            catch (Exception)
            {
                Console.WriteLine("错误了");
                return false;
            }
        }
    }
}
